<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>v-for不要用index作key</title>
</head>

<body>


    <!--载入vue.js-->
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <div id="Application" style="text-align: center">
        <h2>index</h2>
        <ul>
            <li v-for="(item, index) in list" :key="index">
                item: {{item}}
                <button @click="handleRemove(index)">
                    删除
                </button>
            </li>
        </ul>
        <h2>item</h2>
        <ul>
            <li v-for="(item, index) in list" :key="item">
                item: {{item}}
                <button @click="handleRemove(index)">
                    删除
                </button>
            </li>
        </ul>
        <button @click="handleAddd">
            新增
        </button>
    </div>

    <script>
        const App = Vue.createApp({
            setup() {
                const list = Vue.ref(['111', '222', '333', '444'])

                const handleAddd = () => {
                    list.value.unshift('test' + Math.random().toString(16).slice(2))
                }

                const handleRemove = index => {
                    list.value.splice(index, 1)
                }
                return {
                    list,
                    handleAddd,
                    handleRemove
                }
            }
        })
        //将Vue组件绑定到页面上id为Application的元素上
        App.mount("#Application");
    </script>
</body>

</html>